package first_practice;

/**
 * @author ZhiWen Ren
 * @version 1.0
 * @description: LeetCode-108-将有序数组转换为二叉搜索树
 * @date 2025/4/10 11:27
 */
public class Solution108 {
    public TreeNode sortedArrayToBST(int[] nums) {
        return helper(nums, 0, nums.length - 1);
    }

    public TreeNode helper(int[] nums, int left, int right) {
        if (left > right) {
            return null;
        }
        int mid = (left + right + 1) / 2; // 取中间偏右的元素作为根节点
        TreeNode root = new TreeNode(nums[mid]);
        root.left = helper(nums, left, mid - 1); // left和right左右边界始终不变
        root.right = helper(nums, mid + 1, right);
        return root;
    }
}
